package com.hzy.servlet;

import com.hzy.pojo.Fruit;
import com.hzy.service.FruitService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Package: servlet
 * @ClassName: InfoServlet
 * @Author: 862982795
 * @CreateTime: 2021/06/07 9:46
 * @Description:
 */
@WebServlet("/InfoServlet")
public class InfoServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        FruitService service = new FruitService();
        int fruitId = Integer.parseInt(req.getParameter("fruitId"));
        String i = req.getParameter("i");

        Fruit fruit = service.info(fruitId);
        req.setAttribute("fruit", fruit);

        System.out.println("i=" + i);
        if (i.equals("0")) {
            req.getRequestDispatcher("info.jsp").forward(req, resp);
        } else {
            req.getSession().setAttribute("fruitId", fruitId);
            req.getRequestDispatcher("update.jsp").forward(req, resp);
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
